package com.heima.article.fegin;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.IArticleClient;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.AuthorService;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.pojos.ApAuthor;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author 胡慧
 * @title: ArticleClient
 * @projectName heima-leadnews
 * @description: 作者的远程调用
 * @date 2021/2/2023:39
 */
@RestController
public class ArticleClient implements IArticleClient {
    @Autowired
    private AuthorService authorService;

    @GetMapping("/api/v1/author/findByUserId/{userId}")
    public ApAuthor findByUserId(@PathVariable("userId") Integer userId) {
        return authorService.getOne(Wrappers.<ApAuthor>lambdaQuery().eq(ApAuthor::getUserId, userId));
    }

    //    创建作者
    @PostMapping("/api/v1/author/save")
    public ResponseResult save(@RequestBody ApAuthor apAuthor) {
        authorService.save(apAuthor);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApArticleService apArticleService;

    @PostMapping("/api/v1/article/save")
    public ResponseResult saveArticle(ArticleDto articleDto) {
        return apArticleService.saveArticle(articleDto);
    }

    @GetMapping("/api/v1/author/one/{id}")
    public ApAuthor selectById(@PathVariable("id") Integer id) {
        return authorService.getById(id);
    }
}
